package guoanmaker.provider.business.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.provider.business.model.ReturnResult;
import guoanmaker.provider.business.modelnobind.DemandDocking;
import guoanmaker.provider.business.modelnobind.ProvideOrderFormForOrder;
import guoanmaker.provider.business.service.ProvideDemandDockingService;
import guoanmaker.provider.business.service.ProvideSpaceReserveService;
import guoanmaker.provider.helper.common.AjaxJson;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.Jedis;

/**
 * 
* @ClassName: ProvideDemandDockingController 
* @Description: 需求对接controller
* @author Yvonne_MGL
* @date 2017年12月18日 上午午11:24:07 
*  
 */
@RestController
@RequestMapping(ApiController.DEMANDDOCKING_URL)
public class ProvideDemandDockingController {
	
	
	@Resource
	private ProvideDemandDockingService provideDemandDockingService;
	
	@Autowired  
	private  HttpServletRequest request;
	
	/**
	 * 
	* @Description: 根据条件查询需求 
	* @param beginTime 起始时间
	* @param endTime 结束时间
	* @param likeStr 关键字：模糊查询  联系人，联系方式，企业名称，订单编号
	* @param state 1为未确认    2为已确认
	* @param pageNumber 页数
	* @param pageSize 每页条数
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月18日 上午10:57:59     
	* @return Map    
	* @throws 
	 */
	@ApiOperation(value = "查询需求对接", notes = "查询需求对接")
	@RequestMapping(value = "/findByProperties", method = RequestMethod.POST)
	public AjaxJson findByProperties(String beginTime,String endTime,String likeStr,String state,String businessid, Integer pageNumber, Integer pageSize,String userid){
		AjaxJson ajaxJson=new AjaxJson();
		Map demandDockings = new HashMap();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(demandDockings);
		}else{
			try {
				demandDockings = provideDemandDockingService.findByProperties(beginTime, endTime, likeStr, state, businessid, pageNumber, pageSize, userid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(demandDockings);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(demandDockings);
			}
		}
		
		return ajaxJson;

	}
	
	/**
	 * 
	* @Description: 根据id查询需求对接 
	* @param id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月18日 上午11:13:00     
	* @return DemandDocking    
	* @throws 
	 */
	@ApiOperation(value = "查询需求对接", notes = "查询需求对接")
	@RequestMapping(value = "/findDemandDockingByid", method = RequestMethod.POST)
	public AjaxJson findDemandDockingByid(String id){
		AjaxJson ajaxJson=new AjaxJson();
		DemandDocking demandDocking=new DemandDocking();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(demandDocking);
		}else{
			try {
				demandDocking=provideDemandDockingService.findDemandDockingByid(id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(demandDocking);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(demandDocking);
			}
		}
		
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 根据用户id和商品id查询需求对接详情
	* @param @param userid
	* @param @param commodityid
	* @param @return
	* @author Yvonne_MGL 
	* @date 2018年1月10日 上午11:13:53     
	* @return DemandDocking    
	* @throws 
	 */
	@ApiOperation(value = "查询需求对接", notes = "查询需求对接")
	@RequestMapping(value = "/findDemandDockingByCommid", method = RequestMethod.POST)
	public AjaxJson findDemandDockingByCommid(String userid,String commodityid){
		AjaxJson ajaxJson=new AjaxJson();
		DemandDocking demandDocking=new DemandDocking();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(demandDocking);
		}else{
			try {
				demandDocking=provideDemandDockingService.findDemandDockingByCommid(userid, commodityid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(demandDocking);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(demandDocking);
			}
		}
		
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 创建需求对接订单 
	* @param commodityid 商品id
	* @param userid 用户id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param projectIntroduction 项目简介
	* @param enterpriseName 企业名称
	* @param position 职位
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月18日 上午10:14:54     
	* @return ReturnResult    
	* @throws 
	 */
	@ApiOperation(value = "创建需求对接", notes = "创建需求对接")
	@RequestMapping(value = "/createDemandDocking", method = RequestMethod.POST)
	public AjaxJson createDemandDocking(String userid,String commodityid,String enterpriseName,String position,String contactPerson,String contactNumber,String projectIntroduction){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=provideDemandDockingService.createDemandDocking(userid, commodityid, enterpriseName, position, contactPerson, contactNumber, projectIntroduction);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 修改需求对接订单 
	* @param id 对接id
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param projectIntroduction 项目简介
	* @param enterpriseName 企业名称
	* @param position 职位
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月18日 上午10:14:54     
	* @return ReturnResult    
	* @throws 
	 */
	@ApiOperation(value = "修改需求对接", notes = "修改需求对接")
	@RequestMapping(value = "/updateDemandDocking", method = RequestMethod.POST)
	public AjaxJson updateDemandDocking(String id,String enterpriseName,String position,String contactPerson,String contactNumber,String projectIntroduction){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=provideDemandDockingService.updateDemandDocking(id, enterpriseName, position, contactPerson, contactNumber, projectIntroduction);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 确认需求对接订单 
	* @param id 对接id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年12月18日 上午10:14:54     
	* @return ReturnResult    
	* @throws 
	 */
	@ApiOperation(value = "确认需求对接", notes = "确认需求对接")
	@RequestMapping(value = "/confirmDemandDocking", method = RequestMethod.POST)
	public AjaxJson confirmDemandDocking(String id){
		AjaxJson ajaxJson=new AjaxJson();
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=provideDemandDockingService.confirmDemandDocking(id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}

}
